﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web;

namespace WebPartBase
{
    class Helper
    {
        //Enum for log severity level
        public enum LogSeverity
        {

            Info,

            Warning,

            Error

        }

            public static void LogtoList(string title, LogSeverity severity, string description, string message)
            {


                using (SPSite siteCollection = new SPSite(SPContext.Current.Site.ID))
                {

                    using (SPWeb site = siteCollection.OpenWeb(SPContext.Current.Web.ID))
                    {
                        // Logs to Logs list of the site 
                        SPList logsList = site.Lists.TryGetList("Logs");


                        if (logsList == null)
                        {
                            site.Lists.Add("Logs", "List to log exceptions", SPListTemplateType.GenericList);
                            SPList list = site.Lists["Logs"];

                            // create Text type new column called "Title" 
                            SPFieldText fldTitle = (SPFieldText)list.Fields.CreateNewField(
                            SPFieldType.Text.ToString(), " Title ");

                            list.Fields.Add("Severity", SPFieldType.Choice, false);

                            SPFieldChoice fldSeverity = (SPFieldChoice)list.Fields["Severity"];
                            fldSeverity.EditFormat = SPChoiceFormatType.Dropdown;

                            fldSeverity.Choices.Add("Info");
                            fldSeverity.Choices.Add("Warning");
                            fldSeverity.Choices.Add("Error");

                            fldSeverity.Update();
                            list.Fields.Add("Message", SPFieldType.Note, false);

                            //SPFieldMultiLineText fldMessage = (SPFieldMultiLineText)list.Fields["Message"];

                            list.Fields.Add("Description", SPFieldType.Note, false);

                            //SPFieldMultiLineText fldDescription = (SPFieldMultiLineText)list.Fields["Description"];

                            list.Fields.Add("URL", SPFieldType.URL, false);
                            // SPFieldUrl fldURL = (SPFieldUrl)list.Fields["URL"];

                            list.Update();

                            SPListItem newItem = list.Items.Add();

                            newItem["Title"] = title;

                            newItem["Severity"] = severity.ToString();

                            newItem["Description"] = description;

                            newItem["Message"] = message;

                            SPFieldUrlValue pageUrlFieldValue = new SPFieldUrlValue();

                            pageUrlFieldValue.Url = HttpContext.Current.Request.Url.ToString();

                            newItem["URL"] = pageUrlFieldValue;

                            list.Update();

                            //list.AllowDeletion = false;

                            //list.Update();

                        }

                        else
                        {

                            SPListItem newItem = logsList.Items.Add();

                            // adding exception details to below column
                            //TODO: move the names of  column to a common file to retrieve here

                            newItem["Title"] = title;

                            newItem["Severity"] = severity.ToString();

                            newItem["Description"] = description;

                            newItem["Message"] = message;


                            SPFieldUrlValue pageUrlFieldValue = new SPFieldUrlValue();

                            pageUrlFieldValue.Url = HttpContext.Current.Request.Url.ToString();

                            newItem["URL"] = pageUrlFieldValue;

                            newItem.Update();

                            //logsList.AllowDeletion = false;

                            logsList.Update();

                        }
                    }


                // Logs errors to event logs, if any occurs during logging !! Not possible in Sanbox!

            }

        }
        // Function to create custom list
        public static void createList(SPWeb web, string listName)
        {
            try
            {
                using (web)

                    // To check if List already exists

                    foreach (SPList lists in web.Lists)
                    {
                        if (lists.Title == listName)
                        {
                            return;
                        }
                    }

                Guid newListGuid = web.Lists.Add(listName, "custom List", SPListTemplateType.GenericList);
                SPList newList = web.Lists[newListGuid];

            }
            catch(Exception exp)
            {
                LogtoList("Unable to create List", LogSeverity.Error, exp.StackTrace, exp.ToString());
                return;
            }

        }



    }
}
